<?php
class CronCommon extends AppModel
{
	
	var $mail_once = 30;
	var $sleep_time = 60;
	var $log_dir;
	var $working_dir;

	
	function writeLastRun($timestamp = null) {
		$timestamp = ($timestamp) ? $timestamp : time();
		FileManipulator::write($this->log_dir . 'cron_last_run.txt', date('Y-m-d H:i', $timestamp));
	}
	
	function readLastRun() {
		return FileManipulator::read($this->log_dir . 'cron_last_run.txt');
	}	
	
	function &getCategories() { 
		$sql = "SELECT id, parent_id FROM {$this->tbl->category}";
		$result = &$this->db->Execute($sql) or die(db_error($sql));
		while($row = &$result->FetchRow()) {
			$data[$row['id']] = $row;
		}
		
		return $data;
	}
	
	function &getParentCategories($id, $categories) {
		return TreeHelperUtil::getParentsById($categories, $id);
	}
	
	
	function getSummary($str, $num_signs = '150') {
	
		if($num_signs == 'all') { return $str; }
		
		$values = array('<br>', '<BR>',  '< /br>', '<li>', '<p>', '<P>', '  ');
		$str = strip_tags(str_replace($values, ' ', $str)); 
		
		if(strlen($str) < $num_signs) { return $str; }
		
		return substr($str, 0, strrpos(substr($str, 0, $num_signs), ' ')) . '...';
	}
	
	
	function log($string) {
		
		$str = date('Y-m-d H:i:s') . ' - ';
		$str .= $string;
		$str .= "\n";
		return FileManipulator::write($this->log_dir . 'cron_job.log', $str, true);
	}
	
	
	function sleep() {
		static $i = 1;
		
		$pause  = ($i % $this->mail_once) ? false : true;
		if($pause) { 
			sleep($this->sleep_time);
		}
		$i++; 	
	}
}
?>